package org.swfer.UIC 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	
	/**
	 * 控件基类.
	 * @author swfer
	 */
	public class Component 
	{
		/** 控件生存周期监控对象 **/
		public static var lifeCycleManager:ICycle=null;
		
		protected var _view:MovieClip;
		protected var _enabled:Boolean = true;
		protected var disposed:Boolean = false;
		protected var callbackDict:Object = null;
		protected var _injectData:Object = null;
		protected var _injectMethod:Function = null;
		
		/**
		 * constructor
		 * @param	view
		 */
		public function Component(view:MovieClip) 
		{
			if (lifeCycleManager) lifeCycleManager.create(this);
			_injectMethod = injectMx;
			
			_view = view;
			
			if (_view.stage) addToStage();
			else _view.addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		
		//////////////////////////////////////////
		/// methods
		//////////////////////////////////////////
		
		/**
		 * 添加控件试图到指定容器
		 * @param	parent
		 */
		public function addChildTo(parent:DisplayObjectContainer):void
		{
			if (parent) parent.addChild(view);
		}
		
		/** 
		 * 从显示列表移除控件视图 
		 */
		public function removeFromParent():void
		{
			if (view.parent) view.parent.removeChild(view);
		}
		
		/**
		 * 注册事件回调函数
		 * @param	type
		 * @param	handler
		 */
		public function addCallBack(type:String,handler:Function):void
		{
			callbackDict = callbackDict || { };
			var arr:Array = callbackDict[type] || [];
			callbackDict[type] = arr;
			
			if(arr.indexOf(handler)==-1) arr.push(handler);
		}
		
		/**
		 * 移除事件回调函数
		 * @param	type
		 * @param	handler
		 */
		public function removeCallBack(type:String, handler:Function):void
		{
			if (callbackDict == null) return;
			if (callbackDict.hasOwnProperty(type) == false) return;
			
			var arr:Array = callbackDict[type];
			var index:int = arr.indexOf(handler);
			
			if (index == -1) return;
			
			arr.splice(index, 1);
		}
		
		/**
		 * 移除全部回调函数
		 */
		public function removeAllCallBack():void
		{
			callbackDict = null;
		}
		
		/**
		 * 清理释放控件内部引用
		 */
		public function dispose():void
		{
			if (disposed) return;
			disposed = true;
			
			if (lifeCycleManager) lifeCycleManager.destroy(this);
			
			removeEvt();
			removeAllCallBack();
			_view = null;
			_injectData = null;
		}
		
		/**
		 * 控件初始化
		 */
		protected function init():void
		{
			//trace('not implemented');
		}
		
		/**
		 * 事件添加
		 */
		protected function addEvt():void
		{
			//trace('not implemented');
		}
		
		/**
		 * 控件事件移除
		 */
		protected function removeEvt():void
		{
			//trace('not implemented');
		}
		
		/**
		 * 执行回调事件函数
		 * @param	type
		 */
		protected function executeCallBack(type:String):void
		{
			if (callbackDict == null) return;
			if (callbackDict.hasOwnProperty(type) == false) return;
			
			for each (var item:Function in callbackDict[type]) 
			{
				if (Boolean(item)==false) continue;
				
				try 
				{
					item(this);
				}
				catch (err:Error)
				{
					throw new SyntaxError(err.message+' 注册回掉函数调用出错');
				}
				
			}
		}
		
		//////////////////////////////////////////
		/// evt handlers
		//////////////////////////////////////////
		
		/**
		 * 控件初始化延迟到添加到显示列表
		 */
		protected function addToStage(e:Event=null):void 
		{
			_view.removeEventListener(Event.ADDED_TO_STAGE, addToStage);
			
			init();
			addEvt();
		}
		
		//////////////////////////////////////////
		/// getters/setters
		//////////////////////////////////////////
		
		/** 控件是否可用。不可用状态控件变灰，屏蔽鼠标事件 **/
		public function get enabled():Boolean { return _enabled;}
		public function set enabled(value:Boolean):void 
		{
			_enabled = value;
			setEnabled(view,value);
		}
		
		/** [r] 控件皮肤 **/
		public function get view():MovieClip { return _view;}
		
		/**
		 * @notice 该属性设计目的是给复杂控件 视图更新。实际上和对象属性值更新视图存在交叉
		 * 数据注入。匹配到view。每个控件的都有自己的默认注入规则。自定义注入规则调用injectMethod 
		 */
		public function get injectData():Object { return _injectData;}
		public function set injectData(value:Object):void 
		{
			_injectData = value;
			if (_injectMethod == null) return;
			_injectMethod(view,_injectData);
		}
		
		/**
		 * @notice 该属性设计目的是给复杂控件 视图更新。实际上和对象属性值更新视图存在交叉
		 * 数据注入规则 
		 */
		public function get injectMethod():Function { return _injectMethod;}
		public function set injectMethod(value:Function):void 
		{
			_injectMethod = value;
		}
	}

}